<?php
Yii::import('ext.AuthorizeNetProcessor');
class AuthorizeNet extends PaymentAPI{
	
	const API_NAME = 'AuthorizeNet';
	
	private	$_paymentInfo;
	
	private $_emailInfo = array(
			'header'	=>	'<img src="http://www.dibzees.com/images/logo.png" />',
			'footer'	=>	'',
	);
	
	public function apiInit(){
		$this->setApiName(self::API_NAME);
		$this->_account = Yii::app()->params['paymentAPI']['authorizeNet']['account'];
		$this->setAPIobj(new AuthorizeNetProcessor($this->_account['api_login_id'],$this->_account['transaction_key']));
		return $this;
	}
		
	public function apiRequest($request = null){
		$this->getAPIobj()->setSandbox($this->_account['sandBox']);
		$country = Yii::app()->tools->country($this->billingInfo->country);
		$state = Yii::app()->tools->state($this->billingInfo->state);	
		$fields = array(
		        'customer_id'                 =>Yii::app()->user->id, //√
				'amount' 		                  => $this->totalPrice,    //  √
				'card_num' 		              => $this->creditCardInfo->cardNumber,// √
				'exp_date' 		              => date('m/y',$this->creditCardInfo->cardExpDate), //√
				'first_name' 	                  => $this->billingInfo->firstName,//√
				'last_name'		              => $this->billingInfo->lastName,//√
				'address' 		                  => $this->billingInfo->address1,//√
				'city' 			                  => $this->billingInfo->city,//√
				'state' 		                      => !empty($state)?$state->key:-1,//√
				'country' 		                  => $country->key,//√
				'zip'			                      => $this->billingInfo->zip,//√
				'email'			                  => $this->billingInfo->email,//√
				'card_code' 	                  => $this->creditCardInfo->cardCode,//√
				'invoice_num'	              => $this->getPaymentCode(),//√
				'description'	                  => 'eGunner Penny Auctions',//√
				'phone'			                  => $this->billingInfo->phone,//√
				'header_email_receipt'	  =>	$this->_emailInfo['header'],//√
		);
		$this->getAPIobj()->setFields($fields);
		$this->_response = $this->getAPIobj()->authorizeAndCapture();
		
		$fields['account'] 		= $this->_account;
		$fields['response']		= $this->_response;
		return $this->_response;
	}

	public function apiResponse($response = null){
		return $this->_response;
	}
	
	public function apiPaymentVaild(){
	    if ($this->_response!==null){
	        $response=$this->_response->getTransactionResponse();
	        if ($response!==null && $response->getResponseCode()=='1'){
	            return true;
	        }
	        return false;
	    }else{
	        return false;
	    }		
	}
}